import { Message } from "@/utils/message";
import { userInfo } from "@/utils/userInfo";
import { createRouter, createWebHashHistory } from "vue-router";
import { rolePermissions } from "./config";
import { roleType } from "@/store/useRole";

export const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/login",
    name: "login",
    meta: {
      title: "登录",
    },
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/home",
    name: "home",
    meta: {
      title: "首页",
    },
    component: () => import("@/views/home/index.vue"),
  },
  {
    path: "/labquery",
    name: "labquery",
    meta: {
      title: "实验室申请(学生)",
    },
    component: () => import("@/views/student/LabQuery.vue"),
  },
  {
    path: "/labqueryTeacher",
    name: "labqueryTeacher",
    meta: {
      title: "实验室申请(教师)",
    },
    component: () => import("@/views/teacher/LabQuery.vue"),
  },
  {
    path: "/repairlog",
    name: "repairlog",
    meta: {
      title: "报修处理",
    },
    component: () => import("@/views/repairLog/index.vue"),
  },
  {
    path: "/labapply",
    name: "labapply",
    meta: {
      title: "个人借用情况",
    },
    component: () => import("@/views/student/LabApply.vue"),
  },
  {
    path: "/user-manage",
    name: "userManage",
    meta: {
      title: "用户管理",
    },
    redirect: {
      name: "userList",
    },
    children: [
      {
        path: "list",
        name: "userList",
        meta: {
          title: "用户列表",
        },
        component: () => import("@/views/userList/list/index.vue"),
      },
      {
        path: "create",
        name: "userCreate",
        meta: {
          title: "用户编辑",
        },
        component: () => import("@/views/userList/create/index.vue"),
      },
    ],
  },
  {
    path: "/student-lab-apply",
    name: "studentLabApply",
    meta: {
      title: "学生借用审批",
    },
    component: () => import("@/views/admin/studentLabApply.vue"),
  },
  {
    path: "/teacher-lab-apply",
    name: "teacherLabApply",
    meta: {
      title: "教师排课管理",
    },
    redirect: {
      name: "applylist",
    },
    children: [
      {
        path: "applylist",
        name: "applylist",
        meta: {
          title: "教师实验室申请列表",
        },
        component: () => import("@/views/admin/teacherLabApply.vue"),
      },
      {
        path: "arrangeclass",
        name: "arrangeClass",
        meta: {
          title: "排课",
        },
        component: () => import("@/views/admin/arrangeClass.vue"),
      },
    ],
  },
  {
    path: "/semester-manage",
    name: "semesterManage",
    meta: {
      title: "学期管理",
    },
    component: () => import("@/views/semesterManage/index.vue"),
  },
  {
    path: "/lab-manage",
    name: "labManage",
    meta: {
      title: userInfo.value.role === 2 ? "实验室报修" : "实验室管理",
    },
    redirect: {
      name: "labList",
    },
    children: [
      {
        path: "list",
        name: "labList",
        meta: {
          title: "实验室列表",
        },
        component: () => import("@/views/lab-manage/list/index.vue"),
      },
      {
        path: "create",
        name: "labCreate",
        meta: {
          title: "实验室编辑",
        },
        component: () => import("@/views/lab-manage/create/index.vue"),
      },
    ],
  },
  {
    path: "/lab-type-manage",
    name: "labTypeManage",
    meta: {
      title: "实验室类型管理",
    },
    redirect: {
      name: "labTypeList",
    },
    children: [
      {
        path: "list",
        name: "labTypeList",
        meta: {
          title: "实验室类型列表",
        },
        component: () => import("@/views/lab-type/list/index.vue"),
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
const isOpenRouterGuard = true;
router.beforeEach((to, _from, next) => {
  if (!isOpenRouterGuard) {
    next();
    return;
  }
  const index = userInfo.value.role ?? roleType.NotLogin;
  // @ts-ignore
  const permission = rolePermissions[index];
  if (permission && !permission.includes(to.path)) {
    Message("warning", "你小子！不老实!不要自己乱跳路由！");
    // 返回 false 以取消导航
    next("/");
  }
  next();
});
export default router;
